import { defineMock } from '../_base'
import { USER_TOKENS, getDelayTime, isAdmin, resultError, resultSuccess, filterTree } from '../_utils'
import { mapTree } from 'xe-utils'
import userData from '../_data/system_user'
import menuData from '../_data/system_menu'

export default defineMock([
    {
        url: '/user/login',
        method: 'post',
        timeout: getDelayTime(),
        response: ({ body }) => {
            const { username, password } = body
            if (!username) return resultError(null, '用户名不能为空', 50000)
            if (!password) return resultError(null, '密码不能为空', 50000)
            if (username === 'admin' && password === '123456') {
                const token = 'token_admin'
                return resultSuccess({ token })
            }
            if (username === 'user' && password === '123456') {
                const token = 'token_user'
                return resultSuccess({ token })
            }
            return resultError(null, '账号或者密码错误', 50000)
        }
    },
    {
        url: '/user/logout',
        method: 'post',
        timeout: getDelayTime(),
        response: () => {
            return resultSuccess(null)
        }
    },
    {
        url: '/user/getUserInfo',
        method: 'get',
        timeout: getDelayTime(),
        response: ({ headers }) => {
            const token = headers.token
            if (token && USER_TOKENS.includes(token)) {
                const userList = userData.map((i) => ({
                    id: i.id,
                    nickname: i.nickname,
                    avatar: i.avatar,
                    roles: i.roleIds,
                    permissions: i.permissions
                }))
                return resultSuccess(isAdmin(token) ? userList[0] : userList[1])
            } else {
                return resultError(null, 'token失效', 401)
            }
        }
    },
    {
        url: '/user/detail',
        method: 'get',
        timeout: getDelayTime(),
        response: ({ query }) => {
            const { id } = query
            const index = userData.findIndex((i) => i.id === id)
            if (index >= 0) {
                if (userData[index].id === '01') {
                    return resultSuccess(userData[index])
                }
                if (userData[index].id === '02') {
                    return resultSuccess(userData[index])
                }
            } else {
                return resultError(null, '没有该用户', 400)
            }
        }
    },
    {
        url: '/user/list',
        method: 'get',
        timeout: getDelayTime(),
        response: () => {
            return resultSuccess({
                total: userData.length,
                records: userData
            })
        }
    },
    {
        url: '/user/routes',
        method: 'get',
        timeout: getDelayTime(),
        response: ({ headers }) => {
            const token = headers.token
            if (token && USER_TOKENS.includes(token)) {
                const roles = isAdmin(token) ? ['role_admin'] : ['role_user']
                // 如果是超级管理员角色
                if (roles.includes('role_admin')) {
                    const data = filterTree(JSON.parse(JSON.stringify(menuData)), (i) =>
                        [1, 2].includes(i.type))
                    return resultSuccess(data)
                }
                // 如果是普通用户角色
                if (roles.includes('role_user')) {
                    const data = filterTree(JSON.parse(JSON.stringify(menuData)), (i) => {
                        return i.path !== '/system' && i.roles.some((i) => roles.includes(i)) && [1, 2].includes(i.type)
                    })
                    return resultSuccess(data)
                }
                return resultSuccess([])
            } else {
                return resultError(null, 'token失效', 401)
            }
        }
    },
    {
        url: '/system/menu/options',
        method: 'get',
        timeout: getDelayTime(),
        response: ({ headers }) => {
            const data = mapTree(JSON.parse(JSON.stringify(menuData)), (i) => ({
                id: i.id,
                title: i.title,
                children: i.children
            }))
            return resultSuccess(data)
        }
    },
])
